package cn.coder.easyjson.stream;

import java.lang.reflect.Field;
import java.util.Set;

import cn.coder.easyjson.JSONObject;
import cn.coder.easyjson.util.ObjectUtils;

public class JSONWrapper {

	private Object bean;

	public JSONWrapper(Object obj) {
		this.bean = obj;
	}

	public JSONObject fromBean() {
		JSONObject json = new JSONObject();
		Set<Field> fields = ObjectUtils.getDeclaredFields(this.bean.getClass());
		if (fields.size() > 0) {
			try {
				Object temp;
				for (Field field : fields) {
					field.setAccessible(true);
					temp = field.get(this.bean);
					if (temp != null) {
						json.put(field.getName(), temp);
					}
				}
			} catch (IllegalArgumentException | IllegalAccessException e) {
				throw new RuntimeException("Parse object fail", e);
			} finally {
				this.bean = null;
			}
		}
		return json;
	}

}
